package com.sskj.gocoin.v.activity;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.widget.FingerPrinterView;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.ScreenListener;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;
import zwh.com.lib.FPerException;
import zwh.com.lib.IdentificationInfo;
import zwh.com.lib.RxFingerPrinter;

public class SecurityVerifyActivity extends BaseActivity {
    @BindView(R2.id.security_verify_by_pwd)
    TextView mSecurityVerifyByPwd;  // 使用密码解锁
    @BindView(R2.id.fingerPrinterView)
    FingerPrinterView mFingerPrinterView;
    @BindView(R2.id.security_verify_by_fingerprint)
    ImageView securityVerifyByFingerprint;  // 指纹背景图
    private RxFingerPrinter mRxFingerPrinter;
    private ScreenListener screenListener;
    private DisposableObserver<IdentificationInfo> observer;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_security_verify;
    }

    @Override
    public BasePresenter getPresenter() {
        return new BasePresenter();
    }

    @Override
    protected void onViewBinding() {
        screenListener = new ScreenListener(this);
        screenListener.begin(new ScreenListener.ScreenStateListener() {
            @Override
            public void onScreenOn() {// 屏幕打开
                Log.i("info", App.INSTANCE.getString(R.string.fabimineJavaSecurityVerifyActivity2));
            }

            @Override
            public void onScreenOff() { //屏幕关闭
                Log.i("info", App.INSTANCE.getString(R.string.fabimineJavaSecurityVerifyActivity4));
            }

            @Override
            public void onUserPresent() { //解锁
                mFingerPrinterView.setState(FingerPrinterView.STATE_WRONG_PWD);
                Log.i("info", App.INSTANCE.getString(R.string.fabimineJavaSecurityVerifyActivity6));
                mRxFingerPrinter = new RxFingerPrinter(SecurityVerifyActivity.this);
                initFingerprintCore();
            }
        });
        mSecurityVerifyByPwd.setOnClickListener(v -> {
            startActivity(new Intent(this, ZwPwdLoginActivity.class));
        });
        ClickUtil.click(securityVerifyByFingerprint, () -> {
            mRxFingerPrinter = new RxFingerPrinter(SecurityVerifyActivity.this);
            initFingerprintCore();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        screenListener.unregisterListener();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("info", "onPause....");
        mFingerPrinterView.setState(FingerPrinterView.STATE_WRONG_PWD);
        if (mRxFingerPrinter != null) {
            if (observer.isDisposed()) {
                observer.dispose();
            }
            mRxFingerPrinter.stopListening();
            mRxFingerPrinter.dispose();
            mRxFingerPrinter = null;
        } else {
            Log.i("info", App.INSTANCE.getString(R.string.fabimineJavaSecurityVerifyActivity10));
        }
    }

    private void initFingerprintCore() {
        observer = new DisposableObserver<IdentificationInfo>() {
            @Override
            protected void onStart() {
                if (mFingerPrinterView.getState() == FingerPrinterView.STATE_SCANING) {
                    Log.i("info", App.INSTANCE.getString(R.string.fabimineJavaSecurityVerifyActivity12));
                    return;
                } else if (mFingerPrinterView.getState() == FingerPrinterView.STATE_CORRECT_PWD
                        || mFingerPrinterView.getState() == FingerPrinterView.STATE_WRONG_PWD) {
                    Log.i("info", "xixixixixi");
                    mFingerPrinterView.setState(FingerPrinterView.STATE_NO_SCANING);
                } else {
                    Log.i("info", "hahahahaha");
                    mFingerPrinterView.setState(FingerPrinterView.STATE_SCANING);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onNext(IdentificationInfo identificationInfo) {
                if (identificationInfo.isSuccessful()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.zwsuccess));
                    finish();
                } else {
                    mFingerPrinterView.setState(FingerPrinterView.STATE_WRONG_PWD);
                    FPerException exception = identificationInfo.getException();
                    if (exception != null) {
                        Log.i("info", exception.getDisplayMessage());
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.zwagain));
                    }
                }
            }
        };
        //RxfingerPrinter 会自动在 onPause()时暂停指纹监听，onResume()时恢复指纹监听)
        mRxFingerPrinter.begin().subscribe(observer);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
